<!DOCTYPE html>
<html lang="en">

<head>
  <title></title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    #app {
      width: 600px;
      margin: 10px auto;
    }

    .tb {
      border-collapse: collapse;
      width: 100%;
    }

    .tb th {
      background-color: #0094ff;
      color: white;
    }

    .tb td,
    .tb th {
      padding: 5px;
      border: 1px solid black;
      text-align: center;
    }

    .add {
      padding: 5px;
      border: 1px solid black;
      margin-bottom: 10px;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="add">
      品牌名称:
      <input type="text" v-model="addValue">
      <input type="button" value="添加" @click="add">
    </div>
    <div class="add">
      搜索:
      <input type="text" v-model="searchValue" placeholder="请输入品牌名称">
    </div>
    <table class="tb">
      <tr>
        <th>编号</th>
        <th>品牌名称</th>
        <th>创立时间</th>
        <th>操作</th>
      </tr>
      <tr v-for="item in list" :key="item.id">
        <td>{{item.id}}</td>
        <td>{{item.name}}</td>
        <td>{{item.time |formatTime}}</td>
        <td>
          <button @click="del(item.id)">删除</button>
        </td>
      </tr>
      <tr v-if="list.length===0">
        <td colspan="4">没有数据咯~</td>
      </tr>
    </table>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script src="moment.js"></script>
  <script src="./axios.min.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        list: [],
        addValue: '',
        searchValue: ''
      },
      methods: {
        // 获取完整列表
        getList() {
          axios.get('http://localhost:6006/list')
            .then(res => {
              this.list = res.data
            })
            .catch(function (error) {
              console.log(error)
            })
        },
        // 删除
        del(id) {
          axios.delete(`http://localhost:6006/list/${id}`)
            .then(res => {
              this.getList()
            })
            .catch(function (error) {
              console.log(error)
            })
        },
        // 添加品牌
        add() {
          axios.post(`http://localhost:6006/list`, {
            name: this.addValue,
            time: new Date()
          })
            .then(res => {
              this.getList()
            })
            .catch(function (error) {
              console.log(error)
            })
        }
      },
      watch: {
        'searchValue': function (newVal, oldVal) {
          axios.get(`http://localhost:6006/list?name_like=${newVal}`)
            .then(res => {
              this.list = res.data
            })
            .catch(function (error) {
              console.log(error)
            })
        }
      },
      mounted() {
        this.getList()
      },
      filters: {
        formatTime(val) {
          return moment(val).format('YYYY-MM-DD')
        }
      }
    })
  </script>
</body>

</html>